package 类与对象.多态.练习题;
/*
    定义一个Person类 {name,age,job} 初始化Person 对象数组，有3个Person对象

 */
public class demo01 {
    public static void main(String[] args) {

        Person[] arr = new Person[4];
        arr[0] = new Person("小溪",12,"学生");
        arr[1] = new Person("小北1",20,"外卖员");
        arr[2] = new Person("小哥223333",25,"歌手");
        arr[3] = new Person("小天333",19,"教练");

        Person temp = null;
        for(int i = 0; i < arr.length - 1; i++){
            for(int j = 0; j < arr.length - 1 - i; j++ ){
                //根据Person类中的age排序
                //if(arr[j].getAge() > arr[j + 1].getAge()){
                //根据名字的长度排序
                if(arr[j].getName().length() > arr[j + 1].getName().length()){
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for (Person person : arr) {
            System.out.println(person);
        }
    }

}

class Person{
    private String name;
    private int age;
    private String job;

    public Person(String name, int age, String job) {
        this.name = name;
        this.age = age;
        this.job = job;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", job='" + job + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }
}
